package com.roger.myHome.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class ExternalIPUtil {


    public static void main(String[] args) throws Exception{
        http();
    }

    static void getIp1(){
        String ipFetchUrl = "http://httpbin.org/ip";
        String externalIp = "";

        try {
            URL url = new URL(ipFetchUrl);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            // 也可以添加请求头等，根据需要
            // con.setRequestMethod("GET");
            // con.setRequestProperty("User-Agent", "Mozilla/5.0");

            int responseCode = con.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) { // 200
                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                // 解析JSON以获取IP地址
                // 假设返回的JSON格式是 {"origin": "x.x.x.x"}
                // 需要使用JSON解析库，如Jackson或Gson
                // 这里使用简单的字符串操作进行演示
                int startIndex = response.indexOf("\"origin\": \"");
                if (startIndex != -1) {
                    startIndex += "\"origin\": \"".length();
                    int endIndex = response.indexOf("\"", startIndex);
                    if (endIndex != -1) {
                        externalIp = response.substring(startIndex, endIndex);
                    }
                }
            } else {
                System.out.println("GET request not worked");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("External IP: " + externalIp);
    }

    public static String getOutIP() {
        try {
            URL whatismyip = new URL("http://checkip.amazonaws.com");
            BufferedReader in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));

            String ip = in.readLine();
            System.out.println("===" + ip);
            return ip;

        } catch (Exception e) {

        }

        return "";
    }

    private static HttpURLConnection con;

    public static void http() throws Exception {

        URL url = new URL("http://58.210.154.140:22215/systemConfig-service/v2/user/checkLogin/BN00045");
        con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer content = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            content.append(inputLine);
        }
        in.close();
        con.disconnect();

        System.out.println(content.toString());
    }

}
